from threading import Thread
import threading
import time,\
    random


class Counter:

    def __init__(self):
        self.value = 0
        self.lock = threading.Lock()

    def increment(self):
        self.lock.acquire()
        self.value = self.value + 1  # 此操作非线程安全。全局变量
        print("========================")
        value = self.value
        self.lock.release()
        return value


counter = Counter()

"""
需要先识别临界区。
"""


class TheadingTest(Thread):

    def run(self) -> None:
        time.sleep(1)
        value = counter.increment()
        print(value)


if __name__ == '__main__':
    threads = []
    for i in range(100):
        test = TheadingTest()
        test.start()
